const db = require('../db/index')
const util = require('util')
const query = util.promisify(db.query).bind(db)

function getTree(result, keyId) {
    let arr = [];
    for (idx in result) {
        if (result[idx].ps_level == 0) {
            if (keyId.length !== 0 && keyId.indexOf(result[idx].ps_id + '') == -1) {
                continue;
            }
            arr.push({
                'id': result[idx].ps_id,
                'authName': result[idx].ps_name,
                'pid': result[idx].ps_pid,
                "path": result[idx].ps_api_path,
                'children': []
            })
        }
    }
    for (idx in result) {

        if (result[idx].ps_level == 1) {
            for (var j = 0; j < arr.length; j++) {
                if (result[idx]['ps_pid'] == arr[j].id) {

                    if (keyId.length !== 0 && keyId.indexOf(result[idx].ps_id + '') == -1) {
                        continue;
                    }
                    arr[j].children.push({
                        'id': result[idx].ps_id,
                        'authName': result[idx].ps_name,
                        'pid': result[idx].ps_pid,
                        "path": result[idx].ps_api_path,
                        'children': []
                    })
                }
            }

        }
    }
    //生成三级菜单
    for (idx in result) {
        if (result[idx].ps_level == 2) {
            for (var j = 0; j < arr.length; j++) {
                var secondMenu = arr[j].children
                for (var i = 0; i < secondMenu.length; i++) {
                    if (result[idx]['ps_pid'] == secondMenu[i].id) {
                        if (keyId.length !== 0 && keyId.indexOf(result[idx].ps_id + '') == -1) {
                            continue;
                        }
                        secondMenu[i].children.push({
                            'id': result[idx].ps_id,
                            'authName': result[idx].ps_name,
                            'pid': result[idx].ps_pid + ',' + arr[j].id,
                            "path": result[idx].ps_api_path,


                        })
                    }

                }
            }
        }
    }
    return arr


}


exports.getRights = (req, res) => {
        // res.ResData('gfg')
        //接收参数
        //rights/:type路径里传的,type用params
        const type = req.params.type
            //判断参数类型
        if (!type || type != "list" && type != "tree")
            return res.ResData("参数错误")
                //操作数据库
        var sql = "select e.ps_id,e.ps_name,e.ps_pid,e.ps_level,d.ps_api_path from sp_permission e left join sp_permission_api d on(e.ps_id=d.ps_id)"


        // var sql = `select * from sp_permission,sp_permission_api where sp_permission.ps_id=sp_permission_api.ps_id`

        db.query(sql, type, (err, result) => {
            if (err) return res.ResData("获取权限列表失败")
                // console.log(result);
            if (type === 'list') {
                //定义一个数组
                var arr = [];
                for (idx in result) {
                    let permission = result[idx];
                    arr.push({
                        'id': permission.ps_id,
                        'authName': permission.ps_name,
                        'level': permission.ps_level,
                        'pid': permission.ps_pid,
                        "path": permission.ps_api_path


                    })
                }
                res.ResData("获取权限列表成功", 200, arr)

            } else {
                //生成一级菜单
                var arr = getTree(result, [])
                res.ResData(arr)



            }


        })
    }
    //角色列表
exports.rolesList = async(req, res) => {
    //res.ResData("角色列表")
    //角色表查询角色
    var sql1 = "select * from sp_role";
    var sql2 = "select e.ps_id,e.ps_name,e.ps_pid,e.ps_level,d.ps_api_path from sp_permission e left join sp_permission_api d on(e.ps_id=d.ps_id)"
        // db.query(sql1, (err, result1) => {
        //     // console.log(result);
        //     for(idx in result1){
        //         let keyId=result1[idx].ps_ids.split(',');
        //         console.log( keyId);
        //     }
        // })
    try {

        // console.log(result1);
        var result2 = await query(sql2);
        var result1 = await query(sql1);
        let arr = [];
        for (idx in result1) {
            let keyId = result1[idx]['ps_ids'].split(',');

            // console.log(keyId);
            //获取一级菜单

            // for (idx in result2) {
            //     if (result2[idx].ps_level == 0) {
            //         //获取ps_id,判断是否在keyId数组中，
            //         //如果是数组中角色里的一级菜单，不是不操作, keyId.indexOf(ps_id) == -1表示不在其中
            //         let arr1 = []

            //         arr1.push({
            //             'id': result2[idx].ps_id,
            //             'authName': result2[idx].ps_name,
            //             'level': result2[idx].ps_level,
            //             'pid': result2[idx].ps_pid,
            //             'path': result2[idx].ps_api_path,
            //             "children": []
            //         })
            //     }
            // }
            // for (idx in result2) {
            //     if (result2[idx].ps_level == 1) {
            //         for (var j = 0; j < arr.length; j++) {
            //             if (result2[idx]['ps_pid'] == arr[j].id) {
            //                 if (keyId.length !== 0 && keyId.indexOf(ps_id) == -1) {
            //                     continue;
            //                 }
            //                 arr1[j].children.push({
            //                     'id': result2[idx].ps_id,
            //                     'authName': result2[idx].ps_name,
            //                     'level': result2[idx].ps_level,
            //                     'pid': result2[idx].ps_pid,
            //                     'path': result2[idx].ps_api_path,
            //                     "children": []
            //                 })
            //             }


            //         }



            //     }
            // }




            let obj = {
                "id": result1[idx].role_id,
                "roleName": result1[idx].role_name,
                "roleDesc": result1[idx].role_desc,
                "children": []

            }
            let tree = getTree(result2, keyId)
            obj.children = tree
            arr.push(obj)
        }
        res.ResData("获取角色列表成功", 200, arr)


    } catch (err) {
        res.ResData(err)
    }

}